var seckill = {
    url: {
        now: function () {
            return "/seckill/time/mow";
        },
        exposer: function (seckillId) {
            return '/seckill/' + seckillId + '/exposer';
        },
        execution: function (seckillId, md5) {
            return '/seckill/' + seckillId + '/' + md5 + '/execution';
        }
    },
    valdatePhone: function (phone) {
        if (phone && phone.length === 11 && !isNaN(phone)) {
            return true;
        } else {
            return false;
        }
    },
    handleSeckillkill: function (seckillId, node) {
        node.hide().html('<button class="btn btn-primary btn-lg" id="killBtn">开始秒杀</button>');
        $.post(seckill.url.exposer(seckillId), {}, function (result) {
            if (result && result.status) {
                var exposer = result.data;
                if (exposer.exposed) {
                    var md5 = exposer.md5;
                    var killUrl = seckill.url.execution(seckillId,md5);
                    console.log("killUrl=" + killUrl);

                    $('#killBtn').one('click', function () {
                        $(this).addClass('disabled');
                        $.post(killUrl, {}, function (result) {
                            if (result && result.status) {
                                var killResult = result.data;
                                var state = killResult.state;
                                var stateInfo = killResult.stateInfo;
                                node.html('<span class="label label-success">' + stateInfo + '</span>');

                            }else{
                                alert(result.msg);
                            }
                        });
                    });
                    node.show();
                } else {
                    //本地时间与服务器的时间有差异
                    var now = exposer.now;
                    var start = exposer.start;
                    var end = exposer.end;
                    seckill.countdown(seckillId, now, start, end);
                }
            } else {
                console.log('result:' + result.status);
            }
        })
    },
    countdown: function (seckillId, nowTime, startTime, endTime) {
        var seckillBox = $('#seckill-box');
        if (nowTime > endTime) {
            seckillBox.html(' 秒杀结束!');
        } else if (nowTime < startTime) {
            var killTime = new Date(startTime);
            seckillBox.countdown(killTime, function (event) {
                var format = event.strftime('秒杀倒计时：%D天 %H时 %M分 %S秒');
                seckillBox.html(format);
            })
            //时间完成后回调事件
                .on('finish.countdown', function () {
                    seckill.handleSeckillkill(seckillId, seckillBox);
                });
        } else {
            seckill.handleSeckillkill(seckillId, seckillBox);
        }
    },
    //秒杀详情
    detail: {
        init: function (params) {
            var killPhone = $.cookie("killPhone");
            var startTime = params.startTime;
            var entTime = params.endTime;
            var seckillId = params.seckillId;

            if (!seckill.valdatePhone(killPhone)) {
                var killPhoneModal = $('#killPhoneModal');
                killPhoneModal.modal({
                    show: true,//显示弹出层
                    backdrop: 'static',//禁止位置关闭
                    initKeyboardEvent: false//关闭键盘事件
                });
                $('#killPhoneBtn').click(function () {
                    var inputPhone = $('#killphoneKey').val();
                    if (seckill.valdatePhone(inputPhone)) {
                        $.cookie('killPhone', inputPhone, {expires: 7, path: '/seckill'});
                        window.location.reload();
                    } else {
                        $('#killphoneMessage').hide().html('<label class="label label-danger">手机号错误</label>').show(300);
                    }
                });
            }
            $.get(seckill.url.now(), {}, function (result) {
                if (result && result.status) {
                    var nowTime = result.data;
                    seckill.countdown(seckillId, nowTime, startTime, entTime);
                } else {
                    console.log('result=' + result);
                }
            })
        }
    }
}